package 面试经典150题.字符串;

import java.util.Arrays;

/**
 * @author tmh
 * @date 2024/4/9 21:31
 * @description
 */
public class T151反转字符串中的单词 {
    public static void main(String[] args) {
    }

    public String reverseWords(String s) {
        s=s.trim();
        s = s.replaceAll("\\s+", " ");
        StringBuilder builder = new StringBuilder();
        String[] strings = s.split(" ");
        for (int i = strings.length - 1; i >= 0; i--) {
            builder.append(strings[i]);
            if (i != 0) {
                builder.append(" ");
            }
        }
        return builder.toString();
    }
}
